home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-10-16 | 3.8 KB | 185 lines | [TEXT/MPS ] |
- /*------------------------------------------------------------------------------------------
-
- Program: CPlusTESample 2.0
- File: List.cp
- Uses: List.h
-
- by Andrew Shebanow
- of Apple Macintosh Developer Technical Support
-
- Copyright © 1989-1990 Apple Computer, Inc.
- All rights reserved.
-
- ------------------------------------------------------------------------------------------*/
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __EXCEPTIONS__
- #include "Exceptions.h"
- #endif
-
- #include "List.h"
-
- //----------------------------------------------------------------------------
-
- TList::TList()
- {
- fObjects = (ObjectHandle) NewHandle(sizeof(ObjectRef));
-
- fNumObjs = 0;
- }
-
- void TList::InsertFirst(Object* obj)
- {
- long lSize = GetHandleSize((Handle) fObjects);
- SetHandleSize((Handle) fObjects, lSize+sizeof(ObjectRef));
- FailMemError();
- // shift list elements downwards
- BlockMove((Ptr) &((*fObjects)[0]),
- (Ptr) &((*fObjects)[1]),
- fNumObjs*sizeof(ObjectRef));
-
- (*fObjects)[0] = obj;
- fNumObjs++;
- }
-
- void TList::InsertLast(Object* obj)
- {
- long lSize = GetHandleSize((Handle) fObjects);
- SetHandleSize((Handle) fObjects, lSize+sizeof(ObjectRef));
- FailMemError();
- (*fObjects)[fNumObjs] = obj;
- fNumObjs++;
- }
-
- void TList::MoveToFront(Object* obj)
- {
- short i;
-
- // find object in list - return if not present or
- // if object is already at front of list
- if ((i = FindIdx(obj)) <= 0)
- return;
- // shift objects downwards
- BlockMove((Ptr) &((*fObjects)[0]),
- (Ptr) &((*fObjects)[1]),
- (fNumObjs-i-1)*sizeof(ObjectRef));
- (*fObjects)[0] = obj;
- }
-
- void TList::MoveToBack(Object* obj)
- {
- short i;
-
- // find object in list - return if not present or
- // if object is already at back of list
- if (((i = FindIdx(obj)) < 0) || (i == fNumObjs - 1))
- return;
- // shift objects upwards
- BlockMove((Ptr) &((*fObjects)[i+1]),
- (Ptr) &((*fObjects)[i]),
- (fNumObjs-i-1)*sizeof(ObjectRef));
- (*fObjects)[fNumObjs-1] = obj;
- }
-
- void TList::MoveFront(Object* obj)
- {
- short i;
-
- // find object in list - return if not present or
- // if object is already at front of list
- if ((i = FindIdx(obj)) <= 0)
- return;
- // swap the two objects in the list
- Object* tObj = (*fObjects)[i-1];
- (*fObjects)[i-1] = obj;
- (*fObjects)[i] = tObj;
- }
-
- void TList::MoveBack(Object* obj)
- {
- short i;
-
- // find object in list - return if not present or
- // if object is already at back of list
- if (((i = FindIdx(obj)) < 0) || (i == fNumObjs - 1))
- return;
- // swap the two objects in the list
- Object* tObj = (*fObjects)[i+1];
- (*fObjects)[i+1] = obj;
- (*fObjects)[i] = tObj;
- }
-
- void TList::Remove(Object* obj)
- {
- short i;
-
- // find object in list - return if not present
- if ((i = FindIdx(obj)) < 0)
- return;
- // shift down if not the last element
- if (i < (fNumObjs - 1))
- BlockMove((Ptr) &((*fObjects)[i+1]),
- (Ptr) &((*fObjects)[i]),
- fNumObjs*sizeof(ObjectRef));
- fNumObjs--;
- SetHandleSize((Handle) fObjects, fNumObjs*sizeof(ObjectRef));
- }
-
- void TList::RemoveAll()
- {
- SetHandleSize((Handle) fObjects,0);
- fNumObjs = 0;
- }
-
- void TList::Delete(Object* obj)
- {
- Remove(obj);
- delete obj;
- }
-
- void TList::DeleteAll()
- {
- // this is kind of strange. We go through the
- // list and delete all the objects BEFORE we
- // actually get rid of the references in the
- // list. This is ok, because we won't ever look at
- // the values again
- for (short i = 0; i < fNumObjs; i++)
- delete (*fObjects)[i];
- RemoveAll();
- }
-
- Object* TList::At(short idx) const
- {
- if ((idx < 0) || (idx > (fNumObjs-1)))
- return nil;
- return (*fObjects)[idx];
- }
-
- short TList::FindIdx(Object* obj)
- {
- for (short i = 0; i < fNumObjs; i++)
- {
- if ((*fObjects)[i] == obj)
- return i;
- }
- return -1;
- }
-
- //----------------------------------------------------------------------------
-
- TListIterator::TListIterator(const TList* ls) : fList(ls)
- {
- fIdx = 0;
- }
-
-